<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>xeokit Example</title>
    <link href="../css/pageStyle.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js"></script>
    <style>
        #downloadLink {
            position: absolute;
            font-size: 18px;
            left: 75px;
            top: 80px;
            pointer-events: all;
        }

        #myNavCubeCanvas {
            position: absolute;
            width: 250px;
            height: 250px;
            bottom: 100px;
            left: 50px;
            z-index: 200000;
        }
    </style>
</head>
<body>
<input type="checkbox" id="info-button"/>
<label for="info-button" class="info-button"><i class="far fa-3x fa-question-circle"></i></label>
<canvas id="myCanvas"></canvas>
<canvas id="myNavCubeCanvas"></canvas>
<div id="downloadLink"></div>
<div class="slideout-sidebar">
    <img class="info-icon" src="../../assets/images/bcf_logo.png"/>
    <h1>BCFViewpointsPlugin</h1>
    <h2>Loading a BCF Viewpoint</h2>
    <p>The BIM Collaboration Format (BCF) is a structured file format used for issue tracking with a building information model. BCF defines views of a building model and associated information on collisions and errors connected with specific objects in the view. </p>
    <p>In this example, we're loading a BIM model from the file system, then loading a JSON-encoded BCF viewpoint to highlight a simulated issue with our model.</p>
    <h3>Components Used</h3>
    <ul>
        <li>
            <a href="../../docs/class/src/viewer/Viewer.js~Viewer.html" target="_other">Viewer</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/XKTLoaderPlugin/XKTLoaderPlugin.js~XKTLoaderPlugin.html" target="_other">XKTLoaderPlugin</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/BCFViewpointsPlugin/BCFViewpointsPlugin.js~BCFViewpointsPlugin.html" target="_other">BCFViewpointsPlugin</a>
        </li>
    </ul>
    <h3>Tutorials</h3>
    <ul>
        <li>
            <a href="https://www.notion.so/xeokit/Saving-and-Loading-BCF-Viewpoints-800a5479dc8346848165c88b9ec84fcf" target="_other">Saving and Loading BCF Viewpoints</a>
        </li>
    </ul>
    <h3>Assets</h3>
    <ul>
        <li>
            <a href="https://github.com/openBIMstandards/DataSetSchependomlaan" target="_other">Model source</a>
        </li>
    </ul>
</div>

<script type="module">

    //------------------------------------------------------------------------------------------------------------------
    // Import the modules we need for this example
    //------------------------------------------------------------------------------------------------------------------

    import {Viewer, XKTLoaderPlugin, NavCubePlugin, BCFViewpointsPlugin}  from "../../dist/xeokit-sdk.min.es.js";

    //------------------------------------------------------------------------------------------------------------------
    // Create a Viewer
    //------------------------------------------------------------------------------------------------------------------

    const viewer = new Viewer({
        canvasId: "myCanvas",
        transparent: true
    });

    const camera = viewer.camera;

    camera.eye = [-48.93, 50.41, -54.54];
    camera.look = [0, 1.49, 0];
    camera.up = [0.37, 0.83, 0.41];

    new NavCubePlugin(viewer, {
        canvasId: "myNavCubeCanvas",
        color: "lightblue",
        visible: true,           // Initially visible (default)
        cameraFly: true,       // Fly camera to each selected axis/diagonal
        cameraFitFOV: 45,        // How much field-of-view the scene takes once camera has fitted it to view
        cameraFlyDuration: 0.5 // How long (in seconds) camera takes to fly to each new axis/diagonal
    });

    //------------------------------------------------------------------------------------------------------------------
    // Add a GLTFModelsPlugin - we'll use this to load the model geometry and IFC metadata
    //------------------------------------------------------------------------------------------------------------------

    const xktLoader = new XKTLoaderPlugin(viewer);

    //------------------------------------------------------------------------------------------------------------------
    // Add a BCFViewpointsPlugin - we'll use this to load the BCF viewpoint
    //------------------------------------------------------------------------------------------------------------------

    const bcfViewpoints = new BCFViewpointsPlugin(viewer);

    //------------------------------------------------------------------------------------------------------------------
    // Load the XKT model and IFC metadata
    //------------------------------------------------------------------------------------------------------------------

    const sceneModel = xktLoader.load({
        id: "myModel",
        src: "../../assets/models/xkt/v8/ifc/Schependomlaan.ifc.xkt",
        edges: true // Emphasise edges
    });

    //------------------------------------------------------------------------------------------------------------------
    // When the model has loaded, load the BCF viewpoint
    //
    // The BCF viewpoint loads the camera position, creates a clipping plane, and selects several objects
    //------------------------------------------------------------------------------------------------------------------

    sceneModel.on("loaded", () => {
        const bcfViewpoint = getBCFViewpoint();
        bcfViewpoints.setViewpoint(bcfViewpoint);
    });

    //------------------------------------------------------------------------------------------------------------------
    // Create link to download our JSON BCF viewpoint
    // https://stackoverflow.com/questions/2897619/using-html5-javascript-to-generate-and-save-a-file
    //------------------------------------------------------------------------------------------------------------------

    // Create download link
    // https://stackoverflow.com/questions/2897619/using-html5-javascript-to-generate-and-save-a-file

    const pom = document.createElement('a');

    pom.onclick = function () {

        const viewpoint = bcfViewpoints.getViewpoint({ // Options
            spacesVisible: false, // Don't force IfcSpace types visible in viewpoint (default)
            spaceBoundariesVisible: false, // Don't show IfcSpace boundaries in viewpoint (default)
            openingsVisible: false // Don't force IfcOpening types visible in viewpoint (default)
        });

        const viewpointStr = JSON.stringify(viewpoint, null, 4);
        pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(viewpointStr));
    }

    pom.setAttribute('download', "bcfViewpoint.json");
    pom.innerText = "Download BCF Viewpoint JSON";
    document.getElementById("downloadLink").appendChild(pom);

    //------------------------------------------------------------------------------------------------------------------
    // Returns the JSON BCF viewpoint we'll load
    //------------------------------------------------------------------------------------------------------------------

    function getBCFViewpoint() {
        return {
            "perspective_camera": {
                "camera_view_point": {
                    "x": -48.93,
                    "y": 54.54,
                    "z": 50.41
                },
                "camera_direction": {
                    "x": 0.5553804058193083,
                    "y": -0.619056761360823,
                    "z": -0.5552669007292164
                },
                "camera_up_vector": {
                    "x": 0.37,
                    "y": -0.41,
                    "z": 0.83
                },
                "field_of_view": 60
            },
            "clipping_planes": [
                {
                    "location": {
                        "x": 0,
                        "y": 0,
                        "z": 0
                    },
                    "direction": {
                        "x": 0.5,
                        "y": -0.5,
                        "z": 0
                    }
                }
            ],
            "lines": [
                {
                    "start_point": {
                        "x": -30,
                        "y": 0,
                        "z": -30
                    },
                    "end_point": {
                        "x": 30,
                        "y": 0,
                        "z": -30
                    }
                },
                {
                    "start_point": {
                        "x": -30,
                        "y": 0,
                        "z": -30
                    },
                    "end_point": {
                        "x": -30,
                        "y": 0,
                        "z": 30
                    }
                },
                {
                    "start_point": {
                        "x": -30,
                        "y": 0,
                        "z": -24
                    },
                    "end_point": {
                        "x": 30,
                        "y": 0,
                        "z": -24
                    }
                },
                {
                    "start_point": {
                        "x": -24,
                        "y": 0,
                        "z": -30
                    },
                    "end_point": {
                        "x": -24,
                        "y": 0,
                        "z": 30
                    }
                },
                {
                    "start_point": {
                        "x": -30,
                        "y": 0,
                        "z": -18
                    },
                    "end_point": {
                        "x": 30,
                        "y": 0,
                        "z": -18
                    }
                },
                {
                    "start_point": {
                        "x": -18,
                        "y": 0,
                        "z": -30
                    },
                    "end_point": {
                        "x": -18,
                        "y": 0,
                        "z": 30
                    }
                },
                {
                    "start_point": {
                        "x": -30,
                        "y": 0,
                        "z": -12
                    },
                    "end_point": {
                        "x": 30,
                        "y": 0,
                        "z": -12
                    }
                },
                {
                    "start_point": {
                        "x": -12,
                        "y": 0,
                        "z": -30
                    },
                    "end_point": {
                        "x": -12,
                        "y": 0,
                        "z": 30
                    }
                },
                {
                    "start_point": {
                        "x": -30,
                        "y": 0,
                        "z": -6
                    },
                    "end_point": {
                        "x": 30,
                        "y": 0,
                        "z": -6
                    }
                },
                {
                    "start_point": {
                        "x": -6,
                        "y": 0,
                        "z": -30
                    },
                    "end_point": {
                        "x": -6,
                        "y": 0,
                        "z": 30
                    }
                },
                {
                    "start_point": {
                        "x": -30,
                        "y": 0,
                        "z": 0
                    },
                    "end_point": {
                        "x": 30,
                        "y": 0,
                        "z": 0
                    }
                },
                {
                    "start_point": {
                        "x": 0,
                        "y": 0,
                        "z": -30
                    },
                    "end_point": {
                        "x": 0,
                        "y": 0,
                        "z": 30
                    }
                },
                {
                    "start_point": {
                        "x": -30,
                        "y": 0,
                        "z": 6
                    },
                    "end_point": {
                        "x": 30,
                        "y": 0,
                        "z": 6
                    }
                },
                {
                    "start_point": {
                        "x": 6,
                        "y": 0,
                        "z": -30
                    },
                    "end_point": {
                        "x": 6,
                        "y": 0,
                        "z": 30
                    }
                },
                {
                    "start_point": {
                        "x": -30,
                        "y": 0,
                        "z": 12
                    },
                    "end_point": {
                        "x": 30,
                        "y": 0,
                        "z": 12
                    }
                },
                {
                    "start_point": {
                        "x": 12,
                        "y": 0,
                        "z": -30
                    },
                    "end_point": {
                        "x": 12,
                        "y": 0,
                        "z": 30
                    }
                },
                {
                    "start_point": {
                        "x": -30,
                        "y": 0,
                        "z": 18
                    },
                    "end_point": {
                        "x": 30,
                        "y": 0,
                        "z": 18
                    }
                },
                {
                    "start_point": {
                        "x": 18,
                        "y": 0,
                        "z": -30
                    },
                    "end_point": {
                        "x": 18,
                        "y": 0,
                        "z": 30
                    }
                },
                {
                    "start_point": {
                        "x": -30,
                        "y": 0,
                        "z": 24
                    },
                    "end_point": {
                        "x": 30,
                        "y": 0,
                        "z": 24
                    }
                },
                {
                    "start_point": {
                        "x": 24,
                        "y": 0,
                        "z": -30
                    },
                    "end_point": {
                        "x": 24,
                        "y": 0,
                        "z": 30
                    }
                },
                {
                    "start_point": {
                        "x": -30,
                        "y": 0,
                        "z": 30
                    },
                    "end_point": {
                        "x": 30,
                        "y": 0,
                        "z": 30
                    }
                },
                {
                    "start_point": {
                        "x": 30,
                        "y": 0,
                        "z": -30
                    },
                    "end_point": {
                        "x": 30,
                        "y": 0,
                        "z": 30
                    }
                }
            ],
            "bitmaps": [
                {
                    "bitmap_type": "png",
                    "bitmap_data": "",
                    "location": {
                        "x": -15,
                        "y": 10,
                        "z": 0
                    },
                    "normal": {
                        "x": 0,
                        "y": 0,
                        "z": -1
                    },
                    "up": {
                        "x": 0,
                        "y": -1,
                        "z": 0
                    },
                    "height": 24
                }
            ],
            "components": {
                "visibility": {
                    "view_setup_hints": {
                        "spaces_visible": false,
                        "space_boundaries_visible": false,
                        "openings_visible": false
                    },
                    "exceptions": [
                        {
                            "ifc_guid": "22lJvI_rz50umvUizzyEQi",
                            "originating_system": "xeokit.io"
                        },
                        {
                            "ifc_guid": "2dFicWHgz1KPbr5tHWzMcP",
                            "originating_system": "xeokit.io"
                        },
                        {
                            "ifc_guid": "3ZMk1e17XESP$vROePi38_",
                            "originating_system": "xeokit.io"
                        },
                        {
                            "ifc_guid": "0ldLkGDpv5wRT1Q$Q3V2M1",
                            "originating_system": "xeokit.io"
                        },
                        {
                            "ifc_guid": "2Nn5OYLeT9FBcLTcY8oegS",
                            "originating_system": "xeokit.io"
                        },
                        {
                            "ifc_guid": "3c9gfBnpXAtgB1oHgYsgIs",
                            "originating_system": "xeokit.io"
                        }
                    ],
                    "default_visibility": true
                },
                "coloring": [],
                "selection": [
                    {
                        "ifc_guid": "3b2U496P5Ebhz5FROhTwFH",
                        "originating_system": "xeokit.io"
                    },
                    {
                        "ifc_guid": "2MGtJUm9nD$Re1_MDIv0g2",
                        "originating_system": "xeokit.io"
                    },
                    {
                        "ifc_guid": "3IbuwYOm5EV9Q6cXmwVWqd",
                        "originating_system": "xeokit.io"
                    },
                    {
                        "ifc_guid": "3lhisrBxL8xgLCRdxNG$2v",
                        "originating_system": "xeokit.io"
                    },
                    {
                        "ifc_guid": "1uDn0xT8LBkP15zQc9MVDW",
                        "originating_system": "xeokit.io"
                    }
                ]
            },
            "snapshot": {
                "snapshot_type": "png",
                "snapshot_data": ""
            }
        };
    }
</script>

</html>